﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Microsoft.WindowsAzure.StorageClient;
using DeepTime.DataSources.DL;
using System.Drawing;
using System.Drawing.Imaging;

namespace DeepTime.Web
{
    public partial class DeepZoomImage : System.Web.UI.Page
    {
        /// <summary>
        /// Page Load override
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //
            // Check if request is authenticated
            //
            if (!Request.IsAuthenticated)
            {
                //return;
            }

            //
            // Get data from the query string
            //
            string tileLevel = Request.QueryString["tileLevel"];
            if (tileLevel == null)
            {
                return;
            }

            string tilePositionX = Request.QueryString["tilePositionX"];
            if (tilePositionX == null)
            {
                return;
            }

            string tilePositionY = Request.QueryString["tilePositionY"];
            if (tilePositionY == null)
            {
                return;
            }

            string blobUri = Request.QueryString["imageName"];
            if (blobUri == null)
            {
                return;
            }

            //
            // Stream file
            //
            StreamBlob(tileLevel, blobUri, tilePositionX, tilePositionY);
        }

        private void StreamBlob(string tileLevel, string blobUri, string tilePositionX, string tilePositionY)
        {
            //
            // Get blob for passed parameters
            //
            MemoryStream BlobStream = new MemoryStream();
            blobUri += tileLevel + "/" + tilePositionX + "_" + tilePositionY + ".jpg";
            CloudBlockBlob Blob;

            UploadedImageDataSource uploadedImageDataSource = new UploadedImageDataSource();
            Blob = uploadedImageDataSource.Select(blobUri);

            try
            {
                //
                // Dlownload blob data
                //
                Blob.DownloadToStream(BlobStream);

                //
                // Write to response
                //
                Response.BufferOutput = false;
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite(BlobStream.ToArray());
                Response.End();
                //HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            catch (StorageClientException e)
            {
                //Bitmap b = new Bitmap(256, 256);
                //MemoryStream ms = new MemoryStream();
                //b.Save(ms, ImageFormat.Jpeg);
                //byte[] data = ms.ToArray();
                byte[] data = new byte[0];
                //
                // Write to response
                //
                Response.BufferOutput = false;
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite(data);
                Response.End();
                //HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
        }
    }
}